#include "custom_sp.h"

#include <iostream>
#include <fstream>
#include <iterator>
#include <ctime>

using std::string;
using std::cin;
using std::cout;

int main (int argc, char *argv[])
{
    if (argc == 2)
    {
        Dictionary dict;
        std::ifstream input(argv[1], std::ios_base::in);

        clock_t begin = clock();
        dict.loadDictionary(input);
        clock_t end = clock();

        cout << "Dictionary was loaded in " << (end - begin) * 1.0/CLOCKS_PER_SEC << std::endl;

        string word, result;
        int errors = 0;
        int count = 0;
        int time = 0;
        do
        {
            cin >> word >> result;

            begin = clock();
            string guess = dict.nearestNeighbor(word);
            end = clock();
            time += end - begin;

            cout << "[" << (end - begin) * 1.0 / CLOCKS_PER_SEC << " sec] ";

            if (guess != result)
            {
                cout << "word" << '\t' << word << '\t' 
                     << "guess" << '\t' << guess << '\t'
                     << "right answer" << '\t' << result << std::endl;
                errors++;
            }
            else
                cout << "OK" << std::endl;

            count++;

        } while (!cin.eof());

        cout << count << " words." << std::endl;
        cout << errors << " errors." << std::endl;
        cout << "Average time: " << time * 1.0 / (CLOCKS_PER_SEC * count) << std::endl;
    }
    else
    {
        cout << "Usage: " << argv[0] << " filename" << std::endl;
    }
}
